import matplotlib.pyplot as plt
from sklearn.decomposition import PCA
# 鸢尾花数据集 4维 3类
from sklearn.datasets import load_iris

# 以字典形式加载数据集
data = load_iris()
y = data.target
X = data.data
# 加载降维算法
# n_components 主成分的个数 降维后数据的维度
# svd_solver 特征值分解的方法 默认为'auto'
pca = PCA(n_components=2)
# 使用降维算法
reduced_X = pca.fit_transform(X)
# 保存3类数据点
red_x, red_y = [], []
blue_x, blue_y = [], []
green_x, green_y = [], []

for i in range(len(reduced_X)):
    if y[i] == 0:
        red_x.append(reduced_X[i][0])
        red_y.append(reduced_X[i][1])
    elif y[i] == 1:
        blue_x.append(reduced_X[i][0])
        blue_y.append(reduced_X[i][1])
    else:
        green_x.append(reduced_X[i][0])
        green_y.append(reduced_X[i][1])
# 可视化
plt.scatter(red_x, red_y, c='r', marker='x')
plt.scatter(blue_x, blue_y, c='b', marker='D')
plt.scatter(green_x, green_y, c='g', marker='.')
plt.show()
